package com.ftj.service.impl;

import com.ftj.entity.AdminUser;
import com.ftj.mapper.AdminUserMapper;
import com.ftj.service.AdminUserService;
import com.ftj.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 21seu.ftj on 2020/9/13 17:03
 * @author 21seu.ftj
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public AdminUser login(String userName, String password) {
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        return adminUserMapper.login(userName,passwordMD5);
    }

    @Override
    public AdminUser getUserDetailById(Integer loginUserId) {
        return adminUserMapper.getUserDetailById(loginUserId);
    }

    @Override
    public boolean updateName(Integer loginUserId, String loginUserName, String nickName) {
        //只有当前用户非空才能修改
        AdminUser adminUser = adminUserMapper.getUserDetailById(loginUserId);
        if (adminUser != null){
            return adminUserMapper.updateName(adminUser.getAdminUserId(),loginUserName, nickName);
        }
        return false;
    }

    @Override
    public boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        AdminUser adminUser = adminUserMapper.getUserDetailById(loginUserId);
        if (adminUser != null){
            newPassword = MD5Util.MD5Encode(newPassword,"UTF-8");
            System.out.println(newPassword);
            return adminUserMapper.updatePassword(loginUserId,originalPassword,newPassword);
        }
        return false;
    }


}
